<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.Date" import="java.util.Calendar" import="com.almori.hr.timetracker.*" import="java.util.List" import="java.text.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JSP Page</title>
    </head>
    <body>
        
        <style type="text/css">
            #editTable {
                border: 2px solid #CCCCCC;
                border-collapse: collapse;
                width: 700px; 
                font-family: Cambria, Cochin, Georgia, serif;
                font-size: 14px;
                margin-left: auto;
                margin-right: auto;
            }
            #editTable td, #editTable th {
                border: 1px solid #CCC;
                padding: 10px;
            }

            #editTable th:nth-of-type(2) {
                text-align: center;
            }

            #editTable td:nth-of-type(1), #editTable th:nth-of-type(1) {
                width: 85px; 
                text-align: center;
            }

            #editTable th {
                font-weight: bold;
                font-size: 16px;
                /*          background-color: #DDE9FF;*/
                background:#1BB7DF;
                color:#FFFFFF
            }

            #editTable tr:nth-of-type(2n + 2) {
                background-color: #ECF7FF;
            }

            #editTable tr:nth-of-type(2n + 3) {
                background-color: #FBFDFF;
            }
        </style> 

        <table id="editTable">
            <tr>
                <th>Fecha</th>
                <th>Entrada</th>
                <th>Salida</th>
                <th>Razon</th>
            </tr>

            <%
                TimeTrackerBean bean = new TimeTrackerBean();
                SimpleDateFormat ft = new SimpleDateFormat("hh:mm a");
                Calendar cal = Calendar.getInstance();
                int day = cal.get(Calendar.DATE);
                int month = cal.get(Calendar.MONTH) + 1;
                int year = cal.get(Calendar.YEAR);





                // I need the current 15 days of the month and last ones of last month
                if (day < 16) {
                    out.println("Print current 15th days");
                    out.println("<br />");
                    for (int i1 = 0; i1 < day; i1++) {
                        out.println(year + "-" + month + "-" + (i1 + 1));
                        out.println("<br />");
                    }
                    out.println("Print last 15th days");
                    out.println("<br />");
                    if (month == 1) {
                        year--;
                        month = 12;
                    } else {
                        month--;
                    }
                    for (int i = 15; i < cal.getActualMaximum(cal.DAY_OF_MONTH); i++) {
                        out.println(year + "-" + month + "-" + (i + 1));
                        out.println("<br />");
                    }
                } else {
                    for (int i2 = 15; i2 < day; i2++) {
                        List<Timecard> timecards = bean.getDatesByDate(year + "-" + month + "-" + (i2 + 1), 59);

                        if (timecards != null && timecards.size() > 0) {
                           
                            for (int i = 0; i < timecards.size(); i++) {
                                 out.println("<tr>");
                                 
                                 out.println("<td style=\"text-align: center;\">" + year + "-" + month + "-" + (i2 + 1) + "</td>");

                                // out.print(timecards.get(i).getId() + " - ");

                                if (timecards.get(i).getIn() != null) {
                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(i).getIn()) + " </td> ");
                                } else {
                                    out.print("<td></td>");
                                }
                                if (timecards.get(i).getOut() != null) {
                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(i).getOut()) + " </td> ");
                                } else {
                                    out.print("<td></td>");
                                }
                                if (timecards.get(i).getReason() != null) {
                                    out.print("<td style=\"text-align: center;\">" + timecards.get(i).getReason() + " </td> ");
                                } else {
                                    out.print("<td></td>");
                                }

                                out.println("</tr>");

                            }
                        }
                    }
                    for (int i = 0; i < 15; i++){  //Previous week
                                System.out.println(year + "-" + month + "-" + (i + 1));
                                 List<Timecard> timecards = bean.getDatesByDate(year + "-" + month + "-" + (i + 1), 59);

                        if (timecards != null && timecards.size() > 0) {
                           
                            for (int k = 0; k < timecards.size(); k++) {
                                 out.println("<tr>");
                                 
                                 out.println("<td style=\"text-align: center;\">" + year + "-" + month + "-" + (k + 1) + "</td>");

                                // out.print(timecards.get(i).getId() + " - ");

                                if (timecards.get(k).getIn() != null) {
                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(k).getIn()) + " </td> ");
                                } else {
                                    out.print("<td></td>");
                                }
                                if (timecards.get(k).getOut() != null) {
                                    out.print("<td style=\"text-align: center;\">" + ft.format(timecards.get(k).getOut()) + " </td> ");
                                } else {
                                    out.print("<td></td>");
                                }
                                if (timecards.get(k).getReason() != null) {
                                    out.print("<td style=\"text-align: center;\">" + timecards.get(k).getReason() + " </td> ");
                                } else {
                                    out.print("<td></td>");
                                }

                                out.println("</tr>");

                            }
                        }
                    }
                    
                    
                    
                 }
            %>
            <tr>
                <td width="20"></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
        <br /><br />  
    </body>
</html>
